package com.mydemo.admin.controller;

import com.mydemo.admin.service.impl.SysCaptchaService;
import com.mydemo.admin.service.impl.SysLoginService;
import com.mydemo.common.annotation.Log;
import com.mydemo.common.exception.user.UserException;
import com.mydemo.common.penum.BusinessType;
import com.mydemo.common.pojo.LoginBody;
import com.mydemo.common.pojo.RegisterBody;
import com.mydemo.common.pojo.RespResult;
import com.mydemo.common.pojo.SysUser;
import com.mydemo.admin.service.ISysUserService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理控制器
 *
 * @author Last_Island
 * @version 1.0
 */
@SuppressWarnings(value = {"javadoc"})
@RestController
@RequestMapping("/user")
public class SysUserController extends BaseController {
    private final Logger logger = org.slf4j.LoggerFactory.getLogger(SysUserController.class);

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SysCaptchaService sysCaptchaService;

    @Autowired
    private SysLoginService sysLoginService;

    /**
     * 登录
     *
     * @param loginBody 登录信息
     * @return
     */
    @PostMapping("/login")
    @Log(title = "登录", businessType = BusinessType.ASSIGN)
    public RespResult login(@RequestBody LoginBody loginBody) {
        String token = sysLoginService.login(loginBody);
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return RespResult.success("登录成功", token);
    }

    /**
     * 登出
     *
     * @return
     */
    @PostMapping("/logout")
    public RespResult logout() {
        return sysLoginService.logout(getToken());
    }

    /**
     * 注册
     *
     * @param registerUser 注册信息
     * @return
     */
    @PostMapping("/register")
    @Log(title = "注册", businessType = BusinessType.INSERT)
    public RespResult register(@RequestBody RegisterBody registerUser) throws UserException {
        logger.info("registerUser: {}", registerUser);
        return sysLoginService.register(registerUser);
    }

    /**
     * 获取验证码接口
     *
     * @return
     */
    @GetMapping("/imgCaptcha")
    public RespResult imgCaptcha() {
        return sysCaptchaService.getImgCaptcha();
    }

    /**
     * 获取邮箱验证码接口
     *
     * @param email
     * @return
     */
    @GetMapping("/emailCaptcha/{email}")
    public RespResult getEmailCaptcha(@PathVariable("email") String email) {
        return sysCaptchaService.getEmailCaptcha(email);
    }

    /**
     * 获取当前用户信息接口
     *
     * @return
     */
    @GetMapping("/info")
    public RespResult getInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return RespResult.success("获取用户信息成功", authentication.getPrincipal());
    }

    /**
     * 根据查询添加获取用户列表接口
     *
     * @param sysUser 查询条件
     * @return
     */
    @PreAuthorize("@authz.decide('system:user:list')")
    @GetMapping("/list")
    public RespResult list(SysUser sysUser) {
        startPage();
        List<SysUser> list = sysUserService.getList(sysUser);
        return getTabData(list);
    }

    /**
     * 根据id获取用户信息接口
     *
     * @param id
     * @return
     */
    @PreAuthorize("@authz.decide('system:user:get')")
    @GetMapping("/byId/{id}")
    public RespResult getUserById(@PathVariable("id") Long id) {
        return sysUserService.getUserById(id);
    }

    /**
     * 判断用户名是否重复
     *
     * @param username
     * @return
     */
    @GetMapping("/{username}")
    public RespResult userNameNoRepeat(@PathVariable("username") String username) {
        return sysUserService.usernameNoRepeat(username)? RespResult.success("用户名可用") : RespResult.error(400, "用户名重复");
    }

    /**
     * 添加用户
     * @param sysUser
     * @return
     */
    @PreAuthorize("@authz.decide('system:user:add')")
    @PostMapping
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    public RespResult addUser(@RequestBody SysUser sysUser) {
        return sysUserService.addUser(sysUser) ? RespResult.defaultSuccess() : RespResult.defaultError();
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @PreAuthorize("@authz.decide('system:user:del')")
    @DeleteMapping("/{id}")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    public RespResult delUser(@PathVariable("id") Long id) {
        return sysUserService.delUser(id) ? RespResult.defaultSuccess() : RespResult.defaultError();
    }

    /**
     * 修改用户信息
     *
     * @param sysUser
     * @return
     */
    @PutMapping
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    public RespResult updateUserInfo(@RequestBody SysUser sysUser) {
        return sysUserService.updateUserInfo(sysUser) ? RespResult.defaultSuccess() : RespResult.defaultError();
    }

}
